<!--  -->
<template>
  <div class="main">
    <h2>会员管理</h2>
    <div id="search-card">
      <div id="search-content">
        <el-form ref="search" :model="search" :inline="true" :rules="rules">

          <el-form-item label="会员姓名">
            <el-input v-model="search.username" />
          </el-form-item>

          <el-form-item label="会员电话">
            <el-input v-model="search.phone" />
          </el-form-item>

          <el-form-item label="会员身份证号">
            <el-input v-model="search.idCard" />
          </el-form-item>

          <el-form-item label="年龄区间" prop="minAge">
            <el-input v-model="search.minAge" style="width:100px" maxlength="3" onkeyup="this.value=this.value.replace(/[^\d.]/g,'');" />
          </el-form-item>
          -
          <el-form-item prop="maxAge">
            <el-input v-model="search.maxAge" style="width:100px" maxlength="3" onkeyup="this.value=this.value.replace(/[^\d.]/g,'');" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="margin-left:10px" @click="conditionQuery('search')">查找</el-button>
            <el-button icon="el-icon-refresh" circle @click="fetchData()" />
          </el-form-item>

          <!-- <el-form-item label="">
            <el-input v-model="mohu" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="margin-left:10px" @click="mohuQuery">查找</el-button>
            <el-button icon="el-icon-refresh" circle @click="fetchData()" />
          </el-form-item> -->
        </el-form>
      </div>

    </div>
    <div id="table-area">
      <el-table
        v-loading="listLoading"
        :data="list.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >

        <el-table-column
          label="序号"
          type="index"
          align="center"
        >
          <template scope="scope">
            <span>{{ (currentPage - 1) * pagesize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="id"
          label="编号(手机号)"
        />

        <el-table-column
          align="center"
          prop="stuUserName"
          label="会员姓名"
        />

        <el-table-column
          align="center"
          prop="stuIdCard"
          label="身份证号"
        />

        <el-table-column
          align="center"
          prop="stuSex"
          label="性别"
        >
          <template slot-scope="Scope">
            <span v-if="Scope.row.stuSex==1">男</span>
            <span v-if="Scope.row.stuSex==-1">女</span>
          </template>
        </el-table-column>

        <el-table-column
          align="center"
          prop="stuAge"
          label="年龄"
        />

        <el-table-column
          align="center"
          prop="registrationTime"
          label="注册时间"
        />

        <el-table-column
          align="center"
          prop="userStatus"
          label="会员状态"
        >
          <template slot-scope="Scope">
            <el-tag v-if="Scope.row.userStatus===-1" type="danger">失效</el-tag>
            <el-tag v-if="Scope.row.userStatus===0" type="success">正常</el-tag>
          </template>
        </el-table-column>

        <el-table-column align="center" label="操作">
          <template slot-scope="Scope">
            <el-button v-if="Scope.row.userStatus===0" type="danger" icon="el-icon-delete" @click="handleDelete(Scope.row.id,Scope.row.stuUserName)">失效</el-button>
            <el-button v-if="Scope.row.userStatus===-1" type="primary" icon="el-icon-edit" @click="handleUpdate(Scope.row.id,Scope.row.stuUserName)">启用</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div id="pages">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[5,10]"
          :page-size="pagesize"
          layout="total, sizes,prev, pager, next"
          :total="list.length"
          prev-text="上一页"
          next-text="下一页"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import { getAll, updateUserStatus, conditionSearch, mohuSearch } from '@/api/classUser'
export default {
  name: 'ClassUser',
  // import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    // 这里存放数据
    return {
      currentPage: 1, // 默认显示页面为1
      pagesize: 5, //    每页的数据条数
      list: [],
      search: {
        minAge: '',
        maxAge: ''
      },
      mohu: '',
      listLoading: false,
      rules: {
        minAge: [
          { validator: this.validateCom, trigger: 'blur' },
          { validator: this.validateMin, trigger: 'blur' }
        ],
        maxAge: [
          { validator: this.validateCom, trigger: 'blur' },
          { validator: this.validateMax, trigger: 'blur' }
        ]
      }
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.fetchData()
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {},
  // 方法集合
  methods: {
    // 每页下拉显示数据
    handleSizeChange: function(size) {
      this.pagesize = size
    },
    // 点击第几页
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
    },
    handleClick(row) {
      console.log(row)
    },
    fetchData() {
      this.search = {}
      getAll().then((resp) => {
        this.list = resp.obj
      })
    },
    handleUpdate(id, stuUserName) {
      this.$confirm('确定要启用该账户吗？会员姓名为' + stuUserName).then(() => updateUserStatus(id, 0).then((data) => {
        this.$notify({
          title: '操作成功',
          type: 'success'
        })
        this.fetchData()
      }))
    },
    handleDelete(id, stuUserName) {
      this.$confirm('确定要使该账户失效吗？会员姓名为' + stuUserName).then(() => updateUserStatus(id, -1).then((data) => {
        this.$notify({
          title: '操作成功',
          type: 'success'
        })
        this.fetchData()
      }))
    },
    conditionQuery(search) {
      this.$refs[search].validate((valid) => {
        if (valid) {
          console.log(this.search)
          conditionSearch(this.search).then((data) => {
            this.list = data.obj

            this.currentPage = 1
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    mohuQuery() {
      console.log(this.mohu)
      mohuSearch(this.mohu).then((data) => {
        this.list = data.obj
        this.currentPage = 1
      })
    },
    validateCom(rule, value, callback) {
      const one = Number(value)
      if (one < 0) {
        return callback(new Error('年龄必须大于0'))
      } else if (one > 100) {
        return callback(new Error('年龄必须小于100'))
      }
      return callback()
    },
    validateMin(rule, value, callback) {
      const one = Number(value)
      const max = Number(this.search.maxAge)
      if (!max || one < max) {
        return callback()
      }
      return callback(new Error('不得大于最大值'))
    },
    validateMax(rule, value, callback) {
      const one = Number(value)
      const min = Number(this.search.minAge)
      if (!min || one > min) {
        return callback()
      }
      return callback(new Error('不得小于最小值'))
    }
  }
}
</script>
<style  scoped>
 #main{
        margin-left: 5px;
        margin-right: 5px;
    }

    #table-area{

        margin-left: 10px;
        margin-right: 10px;
    }
    #search-card {
        line-height: 35px;
        text-align: right;
        width: 100%;
        height: 70px;
        overflow-x: hidden;
      }
</style>
